<template>
  <el-dialog title="添加选项" :visible.sync="isDialog" width="30%" :before-close="close">
    <el-form :model="form" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
      <el-form-item label="选项名称" prop="name">
        <el-input v-model="form.name"></el-input>
      </el-form-item>
      <el-form-item label="是否启用" prop="enable">
        <el-switch v-model="form.enable"></el-switch>
      </el-form-item>
      <el-form-item label="选项类型" prop="name" >
        <el-select v-model="form.typecode" :disabled="IsDisabledCode" placeholder="请选择类型">
          <el-option v-for="(item, index) in tabs" :key="index" :label="item.typename" :value="item.typecode"></el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="详细信息" prop="detail">
        <el-input type="textarea" v-model="form.detail"></el-input>
      </el-form-item>
      <!-- <el-form-item label="排序" prop="index">
        <el-input @input="(e) => (form.index = Number(e))" v-model="form.index"></el-input>
      </el-form-item> -->
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="close">取 消</el-button>
      <el-button type="primary" @click="confirm">确 定</el-button>
    </span>
  </el-dialog>
</template>

<script>
export default {
  props: {
    isDialog: {
      type: Boolean,
      default: false
    },
    tabDefault: {},
    IsDisabledCode: {
      type: Boolean,
      default: false 
    },
  },
  data() {
    return {
      form: {
        detail: '',
        enable: true,
        index: 0,
        name: '',
        typecode: ''
      },
      rules: {},
      tabs: []
    };
  },
  watch: {
    tabDefault: {
      deep: true,
      immediate: true,
      handler(newVal) {
        this.form.typecode = newVal;
      }
    },
    isDialog: {
      handler(newVal) {
        if(newVal) {
          this.form.detail = '';
          this.form.enable = true;
          this.form.index = 0;
          this.form.name = '';
        }
      }
    }
  },
  methods: {
    async confirm() {
      await this.$api.optionsManage.Install(this.form);
      this.$emit('search');
      this.close();
    },
    close() {
      this.$emit('update:isDialog', false);
    },
    async getEnumType() { // tab
      const { data } = await this.$api.optionsManage.GetEnumType();
      this.tabs = data.data;
    },
  },
  created() {
    this.getEnumType();
  }

};
</script>

<style></style>
